<?php

class HomepageController extends Controller {

    public $layout = 'newlayout';

    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
        $ads = Ads::model()->findAll(); // tao ra dataProvider moi

        $categories = Category::model()->findAll(); // tao ra dataProvider moi
        $listnews = News::model()->findAll(array('order' => 'news_created_at DESC'));


        $login_form = new LoginForm;
        $this->render('index', array(
            'ads' => $ads,
            'categories' => $categories,
            'login_form' => $login_form,
            'listnews' => $listnews,
        ));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionContact() {
        $model = new ContactForm;
        if (isset($_POST['ContactForm'])) {
            $model->attributes = $_POST['ContactForm'];
            if ($model->validate()) {
                $name = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
                $subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
                $headers = "From: $name <{$model->email}>\r\n" .
                        "Reply-To: {$model->email}\r\n" .
                        "MIME-Version: 1.0\r\n" .
                        "Content-type: text/plain; charset=UTF-8";

                mail(Yii::app()->params['adminEmail'], $subject, $model->body, $headers);
                Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }
        $this->render('contact', array('model' => $model));
    }

    public function actionRegister() {
        $model = new User;
        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'register-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];

            // validate user input and redirect to the previous page if valid
            if ($model->validate()) {
                $model->save();
                //  Yii::import('ext.yii-mail.YiiMailMessage');
                // $message = new YiiMailMessage;
                // $message->setBody('Thanks for your registering', 'text/html');
                //  $message->subject = 'Chào mừng bạn đến với SadNews.com';
                //  $message->addTo($model->email);
                //  $message->from = Yii::app()->params['adminEmail'];
                //  Yii::app()->mail->send($message);
                $this->redirect(Yii::app()->homeUrl);
            }
        }
        // display the login form
        $this->render('register', array('model' => $model));
    }

    // Uncomment the following methods and override them if needed
    /*
     * public function filters() { // return the filter configuration for this
     * controller, e.g.: return array( 'inlineFilterName', array(
     * 'class'=>'path.to.FilterClass', 'propertyName'=>'propertyValue', ), ); }
     * public function actions() { // return external action classes, e.g.:
     * return array( 'action1'=>'path.to.ActionClass', 'action2'=>array(
     * 'class'=>'path.to.AnotherActionClass', 'propertyName'=>'propertyValue',
     * ), ); }
     */

    public function actionLogin() {
        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->homeUrl);
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

}